package de.szut.sudoku.gui;

import javax.swing.JPanel;

import java.awt.Font;

import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.SwingConstants;
import javax.swing.Timer;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;

import de.szut.sudoku.gui.menu.MenuBar;
import de.szut.sudoku.gui.sudokufield.SudokuFieldGenerator;
import de.szut.sudoku.gui.sudokufield.SudokuFieldLabel;
import de.szut.sudoku.main.GameController;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * SudokuPanel of the entire GUI
 * 
 * @author dqi12huisinga
 * 
 */
@SuppressWarnings("serial")
public class SudokuPanel extends JPanel {
	private JLabel lblTitle;
	private JMenuBar menuBar;
	private SudokuFieldGenerator sudokuField;
	private Timer timer;

	public Timer getTimer() {
		return timer;
	}

	public void setTimer(Timer timer) {
		this.timer = timer;
	}

	private JButton btnExit;
	private JButton btnStart;

	public JButton getBtnStart() {
		return btnStart;
	}

	public void setBtnStart(JButton btnStart) {
		this.btnStart = btnStart;
	}

	private JLabel lblTimer;

	public JLabel getLblTimer() {
		return lblTimer;
	}

	public void setLblTimer(JLabel lblTimer) {
		this.lblTimer = lblTimer;
	}

	private boolean gameHasStarted;

	public boolean isGameHasStarted() {
		return gameHasStarted;
	}

	public void setGameHasStarted(boolean gameHasStarted) {
		this.gameHasStarted = gameHasStarted;
	}

	private int currentSudokuTime = 0;

	public int getCurrentSudokuTime() {
		return currentSudokuTime;
	}

	public void setCurrentSudokuTime(int currentSudokuTime) {
		this.currentSudokuTime = currentSudokuTime;
	}

	public SudokuFieldGenerator getSudokuField() {
		return sudokuField;
	}

	public void setSudokuField(SudokuFieldGenerator sudokuField) {
		this.sudokuField = sudokuField;
	}

	/**
	 * Generates the SudokuPanel upon generation
	 */
	public SudokuPanel() {
		setBorder(new EmptyBorder(5, 5, 5, 5));
		setLayout(null);

		// ActionListener for the timer
		ActionListener timerListener = new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				currentSudokuTime++;
				lblTimer.setText(currentSudokuTime + "s");
			}
		};

		// Instantiates a new timer and sets the delay for triggering the
		// timerListener to 1s
		timer = new Timer(1000, timerListener);

		gameHasStarted = false;

		lblTitle = new JLabel("Sudoku");
		lblTitle.setFont(new Font("Taffy", Font.PLAIN, 24));
		lblTitle.setHorizontalAlignment(SwingConstants.CENTER);
		lblTitle.setBounds(0, 22, MainFrame.WIDTH - 5, 24);
		add(lblTitle);

		menuBar = new MenuBar();
		add(menuBar);

		// Generates the SudokuField
		sudokuField = new SudokuFieldGenerator(this);
		sudokuField.generateSudokuField();

		btnExit = new JButton();
		btnExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				MainFrame.getInstance().getExitOperator().callExitOperation();
				MainFrame.getInstance().dispose();
			}
		});
		btnExit.setBounds(MainFrame.WIDTH - 115, MainFrame.HEIGHT - 59, 100, 23);
		MainFrame.getInstance().getlContainer()
				.addTranslatableComponent("gui.SudokuPanel.exit", btnExit);
		add(btnExit);
		btnStart = new JButton();
		// MouseListener that is triggered before the ActionListener to show
		// that the field is being generated
		btnStart.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				btnStart.setText(". . .");
			}

		});
		// Generates the field
		btnStart.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (!gameHasStarted) {
					GameController.getInstance().getGame().generateField();
					showVisualField();
				}
			}
		});
		btnStart.setBounds(10, MainFrame.HEIGHT - 59, 120, 23);
		MainFrame.getInstance().getlContainer()
				.addTranslatableComponent("gui.SudokuPanel.start", btnStart);
		add(btnStart);

		lblTimer = new JLabel("[Timer]");
		lblTimer.setHorizontalAlignment(SwingConstants.CENTER);
		lblTimer.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblTimer.setBounds(0, 534, 498, 19);
		lblTimer.setVisible(false);
		add(lblTimer);
	}

	/**
	 * Shows the visual field on the GUI
	 */
	public void showVisualField() {
		SudokuFieldLabel[][] sudokuButtons = sudokuField.getSudokuButtons();
		for (SudokuFieldLabel[] currentLine : sudokuButtons) {
			for (SudokuFieldLabel currentLabel : currentLine) {
				currentLabel.setVisible(true);
			}
		}

		SudokuFieldLabel[][] sudokuNoteButtons = sudokuField
				.getSudokuNoteButtons();
		for (SudokuFieldLabel[] currentLine : sudokuNoteButtons) {
			for (SudokuFieldLabel currentLabel : currentLine) {
				currentLabel.setVisible(true);
			}
		}
		timer.start();
		lblTimer.setText(currentSudokuTime + "s");
		lblTimer.setVisible(true);
		gameHasStarted = true;
		btnStart.setVisible(false);
	}

	public void startTimer() {

	}
}
